package com.fanrenqiji.common.persistence;

import com.fanrenqiji.modules.sys.entity.User;
import com.fanrenqiji.modules.sys.utils.UserUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;

import javax.annotation.PreDestroy;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月16日 上午10:42:20
 * @description
 * 对应数据表中的Entity基本抽象类
 */
@MappedSuperclass
public class DataEntity extends BaseEntity implements Serializable{

	private static final long serialVersionUID = 1L;
	/**
	 * 备注
	 */
	protected String remarks;	
	/**
	 * 创建对象
	 */
	protected User createBy;	
	/**
	 * 创建日期
	 */
	protected Date createDate;
	/**
	 * 更新者
	 */
	protected User updateBy;
	/**
	 * 更新日期
	 */
	protected Date updateDate;
	/**
	 * 状态标记：删除标记（0：正常；1：删除；2：审核）
	 */
	protected String delFlag;
	
	public DataEntity() {
		super();
		//初始化时候默认是正常的
		this.delFlag = DEL_FLAG_NORMAL;
	}
	/**
	 * 持久化之前对操作对象初始化
	 */
	@PrePersist
	public void prePersist() {
		User user = UserUtils.getUser();
		if (user.getId() != null){
			this.updateBy = user;
			this.createBy = user;
		}
		this.updateDate = new Date();
		if(null ==this.createDate)
		this.createDate = this.updateDate;
	}
	/**
	 * 在更新之前的初始化
	 */
	@PreUpdate
	public void preUpdate() {
		User user = UserUtils.getUser();
		if (user.getId() != null){
			this.updateBy = user;
		}
		this.updateDate = new Date();
	}
	/**
	 * 删除之前的初始化
	 */
	@PreDestroy
	public void preDestroy() {
		
	}
	
	@Length(min=0, max=255)
	@Column(name="remarks")
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@JsonIgnore
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "create_by")
	@NotFound(action = NotFoundAction.IGNORE)
	public User getCreateBy() {
		return createBy;
	}
	public void setCreateBy(User createBy) {
		this.createBy = createBy;
	}

	@JsonIgnore
	@ManyToOne(fetch=FetchType.LAZY)
	@NotFound(action = NotFoundAction.IGNORE)
	@JoinColumn(name = "update_by")
	public User getUpdateBy() {
		return updateBy;
	}
	public void setUpdateBy(User updateBy) {
		this.updateBy = updateBy;
	}

	@Column(name="update_date")
	public Date getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="create_date")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	@Length(min=1, max=1)
	@Column(name="del_flag")
	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}
	
		
}
